//
//  PhoneLogInViewController.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/31.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON


class PhoneLogInViewController: MainViewController {
    var phoneLogView:PhoneLogInView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBarHidden = false
        self.title = "手机登录"
        self.addPhoneLogInView()
        self.addtargetToView()
    }
}

//MARK:--UI
extension PhoneLogInViewController{
    //添加view进来
    func addPhoneLogInView(){
        phoneLogView = PhoneLogInView(frame: MainBounds)
        self.view.addSubview(phoneLogView)
    }
    
    //给各个控件添加点击事件
    func addtargetToView(){
        //登录按钮添加点击事件
        phoneLogView.logButton.addTarget(self, action: #selector(logButtonClick(_:)), forControlEvents: .TouchUpInside)
        
        //短信验证码添加点击事件
        let tapG = UITapGestureRecognizer(target: self, action: #selector(self.smsLogLabelClick(_:)))
        phoneLogView.smsLogLabel.addGestureRecognizer(tapG)
        
        //忘记密码 添加点击事件
        let tapF = UITapGestureRecognizer(target: self, action: #selector(self.forgotPasswordLabelClick(_:)))
        phoneLogView.forgotPassword.addGestureRecognizer(tapF)
        
    }
}

//MARK:--点击事件
extension PhoneLogInViewController{
    //登录按钮点击事件
    func logButtonClick(sender:UIButton){
        let phoneNum = phoneLogView.phoneNumber
        let password = phoneLogView.password
        if phoneNum?.text?.characters.count != 11{
            showHUD.showTopHud(self.view, text: "请输入正确的11位手机号码")
        }else if password?.text?.characters.count<6 || password?.text?.characters.count>20{
            showHUD.showTopHud(self.view, text: "密码必需大于6位小于20位")
        }else{
            phoneNum?.resignFirstResponder()
            password?.resignFirstResponder()
            let hud = showHUD.showWaitHud(self.view) as! WaitHUD
            self.logInAlamofire(phoneNum.text!, password: password.text!, hud: hud)
        }
    }
    
    //短信验证点击事件
    func smsLogLabelClick(sender:UITapGestureRecognizer){
        self.navigationController?.pushViewController(SMSLogInViewController(), animated: true)
    }
    
    //忘记密码点击事件
    func forgotPasswordLabelClick(sender:UITapGestureRecognizer){
        self.navigationController?.pushViewController(ForgotPasswordViewController(), animated: true)
    }
}


//MARK:--Alamofire
extension PhoneLogInViewController{
    //登录的Alamofire
    func logInAlamofire(phone:String,password:String,hud:WaitHUD){
        let parameters = ["phone":phone,"password":password]
        Alamofire.request(.POST, "http://srxapp.zhekd.com/ios.php/Login/login", parameters: parameters, encoding: .JSON).responseJSON { (data) in
            let json = JSON(data: data.data!)
            print(json)
            let code = json["code"].stringValue
            let message = json["message"].stringValue
            if code == "1"{
                hud.alpha = 0
                let token = json["token"].stringValue
                let userID = json["user_id"].stringValue
                let userInfo = UserInfo(token: token,userID: userID, phoneNum: phone, password: password, userName: "YOUKNOWWHO", avatar: "", gender: "YOUKNOWSEX", birthday: "YOUKNOWDAY", signature: "YOUKNOWENHENG")
                OperateUserDefaults.deleteUserDefault()
                OperateUserDefaults.saveUserDefault(userInfo)
                dispatch_async(dispatch_get_main_queue(), {
                    NSNotificationCenter.defaultCenter().postNotificationName(Notifiction.LOGIN, object: nil)
                    self.navigationController?.popToRootViewControllerAnimated(true)
                })
            }else{
                hud.alpha = 0
                showHUD.showTopHud(self.view, text: message)
            }
        }
    }
}









